<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Backend_Models extends Controller_Backend {
	
	public function action_index()
	{
		$view = new View('backend/models.index');
		
		$columns = ORM::factory('Model')->table_columns();
		$view->columns = $columns;
		
		$models = ORM::factory('Model')->order_by('sort', 'asc')->find_all();
		$view->models = $models;
		
		$info = ORM::factory('Info')->where('unique', '=', 'models')->find();
		$view->info = $info;
		
		if (isset($_POST['model']))
		{
			$info = ORM::factory('Info')->where('unique', '=', 'models')->find();
			$info->unique = 'models';
			$info->values($_POST['model'])->save();
			Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'models')));
		}
		
		$this->template->content = $view;
	}
	
	public function action_edit()
	{
		$view = new View('backend/models.edit');
		
		$id = Request::current()->param('par1');
		
		$columns = ORM::factory('Model')->table_columns();
		$view->columns = $columns;
		
		$model = ORM::factory('Model')->where('id', '=', $id)->find();
		$view->model = $model;
		
		if (isset($_POST['model']))
		{
			$model->values($_POST['model'])->save();
			$this->_upload_model_files($model);
			
			if (isset($_POST['save_and_return']))
				Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'models')));
			else
				Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'models', 'action' => 'edit', 'par1' => $model->id)));
		}
		
		$this->template->content = $view;
	}
	
	public function action_delete()
	{
		$id = Request::current()->param('par1');
		ORM::factory('Model')->where('id', '=', $id)->find()->delete();
		Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'models')));
	}
	
	protected function _upload_model_files($model)
	{
		if (isset($_FILES['model']))
		{
			if ( ! is_dir(DOCROOT.'uploads/models'))
			{
				mkdir(DOCROOT.'uploads/models');
				chmod(DOCROOT.'uploads/models', 0777);
			}
			
			foreach ($_FILES['model'] as $key => $data)
			{
				foreach ($data as $column => $value)
				{
					if ( ! empty($_FILES['model']['tmp_name'][$column]))
					{
						$filename = $_FILES['model']['name'][$column];
						$filename = URL::title(UTF8::substr($filename, 0, UTF8::strrpos('.', $filename)), '-', TRUE) .'.'. UTF8::substr($filename, UTF8::strrpos('.', $filename));
						$filename = '/models/'. UTF8::substr($filename, 1);
						
						if (file_exists(DOCROOT.'uploads'.$filename)) unlink(DOCROOT.'uploads'.$filename);
						copy($_FILES['model']['tmp_name'][$column], DOCROOT.'uploads'.$filename);
						
						$model->{$column} = $filename;
						$model->save();
					}
				}
			}
		}
	}
	
	public function action_modifications()
	{
		$view = new View('backend/models.modifications.index');
		
		$columns = ORM::factory('Modification')->table_columns();
		$view->columns = $columns;
		
		$models = ORM::factory('Model')->order_by('sort', 'asc')->find_all();
		$view->models = $models;
		
		$modifications = ORM::factory('Modification')->order_by('sort', 'asc')->find_all();
		$view->modifications = $modifications;
		
		$info = ORM::factory('Info')->where('unique', '=', 'modifications')->find();
		$view->info = $info;
		
		if (isset($_POST['modification']))
		{
			$info = ORM::factory('Info')->where('unique', '=', 'modifications')->find();
			$info->unique = 'modifications';
			$info->values($_POST['modification'])->save();
			
			Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'models', 'action' => 'modifications')));
		}
		
		$this->template->content = $view;
	}
	
	public function action_modification_edit()
	{
		$view = new View('backend/models.modifications.edit');
		
		$id = Request::current()->param('par1');
		
		$columns = ORM::factory('Modification')->table_columns();
		$view->columns = $columns;
		
		$modification = ORM::factory('Modification')->where('id', '=', $id)->find();
		$view->modification = $modification;
		
		$model = ORM::factory('Model')->where('id', '=', $modification->model_id)->find();
		$view->model = $model;
		
		if (isset($_POST['modification']))
		{
			$modification->values($_POST['modification'])->save();
			$this->_upload_modification_files($modification);
			
			if (isset($_POST['save_and_return']))
				Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'models', 'action' => 'modifications')));
			else
				Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'models', 'action' => 'modification_edit', 'par1' => $modification->id)));
		}
		
		$this->template->content = $view;
	}
	
	public function action_modification_delete()
	{
		$id = Request::current()->param('par1');
		ORM::factory('Modification')->where('id', '=', $id)->find()->delete();
		Request::current()->redirect(Route::get('backend')->uri(array('controller' => 'models', 'action' => 'modifications')));
	}
	
	protected function _upload_modification_files($modification)
	{
		if (isset($_FILES['modification']))
		{
			if ( ! is_dir(DOCROOT.'uploads/modifications'))
			{
				mkdir(DOCROOT.'uploads/modifications');
				chmod(DOCROOT.'uploads/modifications', 0777);
			}
			
			foreach ($_FILES['modification'] as $key => $data)
			{
				foreach ($data as $column => $value)
				{
					if ( ! empty($_FILES['modification']['tmp_name'][$column]))
					{
						$filename = $_FILES['modification']['name'][$column];
						$filename = URL::title(UTF8::substr($filename, 0, UTF8::strrpos('.', $filename)), '-', TRUE) .'.'. UTF8::substr($filename, UTF8::strrpos('.', $filename));
						$filename = '/modifications/'. UTF8::substr($filename, 1);
						
						if (file_exists(DOCROOT.'uploads/'.$filename)) unlink(DOCROOT.'uploads'.$filename);
						copy($_FILES['modification']['tmp_name'][$column], DOCROOT.'uploads'.$filename);
						
						$modification->{$column} = $filename;
						$modification->save();
					}
				}
			}
		}
	}
	
}